package com.grace.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.grace.common.JsonData;
import com.grace.dto.AclModuleLevelDto;
import com.grace.model.SysUser;
import com.grace.param.RoleParam;
import com.grace.service.*;
import com.grace.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/sys/role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysTreeService sysTreeService;

    @Autowired
    private SysRoleAclService sysRoleAclService;

    @Autowired
    private SysRoleUserService sysRoleUserService;

    @Autowired
    private SysUserService sysUserService;

    /**
     * page页面
     */
    @RequestMapping("/role.page")
    @ResponseBody
    public ModelAndView page() {
        return new ModelAndView("role");
    }

    /**
     * 新增操作
     */
    @RequestMapping("/save.json")
    @ResponseBody
    public JsonData save(RoleParam param) {
        sysRoleService.save(param);
        return JsonData.success();
    }

    /**
     * 更新操作
     */
    @RequestMapping("/update.json")
    @ResponseBody
    public JsonData update(RoleParam param) {
        sysRoleService.update(param);
        return JsonData.success();
    }

    /**
     * 获取角色列表
     */
    @RequestMapping("/list.json")
    @ResponseBody
    public JsonData list() {
        return JsonData.success(sysRoleService.getAll());
    }

    /**
     * 角色对应的权限树
     */
    @RequestMapping("/roleTree.json")
    @ResponseBody
    public JsonData roleTree(@RequestParam("roleId") int roleId) {
        List<AclModuleLevelDto> roleTreeList = sysTreeService.roleTree(roleId);
        return JsonData.success(roleTreeList);
    }

    /**
     * 用户选择权限保存方法
     */
    @RequestMapping("/changeAcls.json")
    @ResponseBody
    public JsonData changeAcls(@RequestParam("roleId") int roleId, @RequestParam(value = "aclIds", required = false) String aclIds) {
        //如果是空串，将会得到一个不含任何元素的List
        List<Integer> aclIdList = StringUtil.splitToListInt(aclIds);
        sysRoleAclService.changeRoleAcls(roleId, aclIdList);
        return JsonData.success();
    }

    @RequestMapping("/users.json")
    @ResponseBody
    public JsonData users(@RequestParam("roleId") int roleId) {
        //拿到已选的用户列表
        List<SysUser> selectedUserList = sysRoleUserService.getListByRoleId(roleId);
        //进行流式遍历（进行过滤，把状态值非1的过滤出去）但我们这里不考虑
        //selectedUserList=selectedUserList.stream().filter(sysUser -> sysUser.getStatus()!=1).collect(Collectors.toList());

        //拿到未选的用户列表（总用户列表-已选用户列表）
        //1.总用户列表
        List<SysUser> allUserList = sysUserService.getAll();
        List<SysUser> unselectedUserList = Lists.newArrayList();
        //使用流遍历方式，加速
        Set<Integer> selectedUserIdSet = selectedUserList.stream().map(sysUser -> sysUser.getId()).collect(Collectors.toSet());
        for (SysUser sysUser : allUserList) {
//            if (sysUser.getStatus() == 1 && !selectedUserList.contains(sysUser)) { //1为正常状态 //这里太慢了 我们换成流式
            if (sysUser.getStatus() == 1 && !selectedUserIdSet.contains(sysUser.getId())) { //1为正常状态
                unselectedUserList.add(sysUser);
            }
        }
        //我们把已选和未选都通过一个map返回出去
        Map<String, List<SysUser>> map = Maps.newHashMap();
        map.put("selected", selectedUserList);
        map.put("unselected", unselectedUserList);
        return JsonData.success(map);
    }

    /**
     * 角色选择用户保存方法
     */
    @RequestMapping("/changeRoleUsers.json")
    @ResponseBody
    public JsonData changeRoleUsers(@RequestParam("roleId") int roleId, @RequestParam(value = "userIds", required = false) String userIds) {
        //如果是空串，将会得到一个不含任何元素的List
        List<Integer> userIdList = StringUtil.splitToListInt(userIds);
        sysRoleUserService.changeRoleUsers(roleId, userIdList);
        return JsonData.success();
    }


}
